package com.wbpay.sdk.widget;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.WindowManager;

import com.wbpay.sdk.constant.Constants;
import com.wbpay.sdk.utils.Log;
import com.wbpay.sdk.wrapper.ActivityWrapper;

/**
 * 久游activity
 */

public class JyouActivity extends Activity {

    private ActivityWrapper mActivityWrapper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String className = null;
        String callbackAction = null;
        Intent intent = getIntent();
        if (intent != null) {
            callbackAction = intent.getStringExtra(Constants.EXTRA_CALLBACK);
            if (TextUtils.isEmpty(callbackAction)) {
                throw new IllegalArgumentException("miss callback arguement");
            }

            className = intent.getStringExtra(Constants.EXTRA_AWCN);
            if (TextUtils.isEmpty(className)) {
                throw new IllegalArgumentException("miss className arguement");
            }
            try {
                Class<?> cls = Class.forName(className);
                mActivityWrapper = (ActivityWrapper) cls.newInstance();
                mActivityWrapper.setActivity(this);
                mActivityWrapper.setCallbackAction(callbackAction);
            } catch (Exception e) {
                Log.d(Log.TAG, "error : " + e);
            }
            boolean fullscreen = intent.getBooleanExtra(Constants.EXTRA_FULLSCREEN, false);
            if (fullscreen) {
                getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                        WindowManager.LayoutParams.FLAG_FULLSCREEN);
            }
        }
        if (mActivityWrapper != null) {
            mActivityWrapper.onCreate(savedInstanceState);
        } else {
            finish();
            return;
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        if (mActivityWrapper != null) {
            mActivityWrapper.onStart();
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (mActivityWrapper != null) {
            mActivityWrapper.onRestart();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (mActivityWrapper != null) {
            mActivityWrapper.onResume();
        }
    }

    @Override
    protected void onPause() {
        if (mActivityWrapper != null) {
            mActivityWrapper.onPause();
        }
        super.onPause();
    }

    @Override
    protected void onStop() {
        if (mActivityWrapper != null) {
            mActivityWrapper.onStop();
        }
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        if (mActivityWrapper != null) {
            mActivityWrapper.onDestroy();
        }
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (mActivityWrapper != null && mActivityWrapper.onKeyDown(keyCode, event)) {
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void onBackPressed() {
        if (mActivityWrapper == null || !mActivityWrapper.onBackPressed()) {
            super.onBackPressed();
        }
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(0, 0);
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (mActivityWrapper != null) {
            mActivityWrapper.onConfigurationChanged(newConfig);
        }
    }
}
